import time
import os
from multiprocessing import Process,Queue,Lock
from threading import Thread
#进程开启2个 子进程 每个子进程开启 若干个线程 通信


class makeCake(Thread) :
    def __init__(self,q):
        super(makeCake, self).__init__()
        self.q = q

    def run(self) :
        i = 0
        while i < 5 :
            time.sleep(2)
            print('%s做蛋糕'% self.name)
            self.q.put('蛋糕')
            i+=1
        self.q.put(None)


class sendCake(Thread) :
    def __init__(self, q):
        super(sendCake, self).__init__()
        self.q = q

    def run(self):
        while True :
            time.sleep(1)
            if not self.q.empty() :
                key = self.q.get()
                if key == None :
                    break
                else :
                    print('%s送%s' % (self.name, key))
            else :
                print('%s 在等待'% self.name)



def Sub1(q,num):
    print('子线程Sub1父进程%s 参数：%s ' % (os.getppid(), num))
    ts = [makeCake(q,) for i in range(3)]
    for theard in ts :
        print(theard.name)
        theard.start()
    for theard in ts :
        theard.join()


def Sub2(q,num):
    print('子线程Sub2父进程%s 参数：%s '%(os.getppid(),num))
    ts = [sendCake(q, ) for i in range(3)]
    for theard in ts:
        print(theard.name)
        theard.start()
    for theard in ts:
        theard.join()



def heard() :
    print('---开启一个进程---')
    print('Pid',os.getpid())
    q = Queue()
    p1 = Process(target=Sub1,args=(q,2))
    p2 = Process(target=Sub2,args=(q,4))
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    print('---所有进程结束———')
    print(f'有{q.qsize()}块蛋糕')


if __name__ =='__main__' :

    heard()







